const toast = title => {
  wx.showToast({
    title,
    icon: 'none'
  })
}

export default params => {
  const {
    timeStamp = '',
    nonceStr = '',
    package: packageStr = '',
    paySign = ''
  } = params

  return new Promise((resolve, reject) => {
    wx.requestPayment({
      timeStamp: String(timeStamp),
      nonceStr,
      package: packageStr,
      signType: 'MD5',
      paySign,
      success(res) {
        toast('支付成功')
        resolve(res)
      },
      fail({ errMsg }) {
        const isCancel = errMsg === 'requestPayment:fail cancel'
        const message = isCancel ? '支付已取消' : '支付失败'
        toast(message)
        reject(message)
      }
    })
  })
}
